<?php
// 应用公共文件

if (!function_exists('tp')) {
    /**
     * \App\Common\Util\TP 类的函数封装，
     * 主要用于视图取消命名空间的编写，缩短长度
     *
     * @return mixed|\App\Common\Util\RouteUtil|\App\Request
     */
    function tp(#[\JetBrains\PhpStorm\ExpectedValues([
        'route',
        'request',
        'log',
        'cache',
        'session',
        'config',
        'instance',
        'event',
        'commandOutput',
        'isDev',
    ])] $type = null, ...$params): mixed
    {
        if (!in_array($type, ['route', 'request', 'log', 'cache', 'session', 'config', 'instance', 'event', 'commandOutput', 'isDev'])) {
            $params = [$type, ...$params];
            $type = 'route';
        }

        return \App\Common\Util\TP::$type(...$params);
    }
}


if (!function_exists('op_log')) {
    /**
     * 手动记录操作日志
     *
     * @param string $des
     * @param array  $data
     * @param array  $exception
     * @param mixed  $userInfo
     *
     * @return void
     */
    function op_log(string $des, array $data = [], array $exception = [], mixed $userInfo = []): void
    {
        /**
         *
         * @param string $des
         * @param array  $data
         * @param string $model
         * @param int    $userId
         * @param array  $userInfo
         * @param array  $exception
         */
        $classpath = "\\Plugins\\OperationLogs\\Common\\Service\\OperationLogService";
        if (class_exists($classpath)) {
            $userId = 0;
            $model = '';
            if (!is_array($userInfo)) {
                try {
                    $userId   = $userInfo->id;
                    $model    = $userInfo::class;
                    $userInfo = $userInfo->toArray();
                } catch (\Exception $e) {
                    \think\facade\Log::error($e->getMessage());
                }
            }

            call_user_func($classpath . "::write", $des, $data, $model, $userId, $userInfo, $exception);
        }
    }
}
